package com.ly.hotel.spainterface.nm;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.spainterface.nm.bean.NmXFBuyItemBean;
import com.ly.hotel.spainterface.nm.event.XFBuyResEvent;
import com.ly.hotel.spainterface.nm.event.XFCartOperationCartEvent;
import com.ly.hotel.spainterface.nm.event.XFUpdateCartEvent;
import com.ly.hotel.util.ToastUtil;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by SHUBEN on 2018/1/26.
 */

public class XFShopCartDialog extends Dialog {
    private NmXiaoFeiActivity mcontext;
    private List<NmXFBuyItemBean> buylist;
    private ListView cartList;
    private XFShopCartAdapter xfShopCartAdapter;
    private TextView xfPricetext;
    private Button xfEmptyCart;
    private Button xfBuyNowCart;


    public XFShopCartDialog(NmXiaoFeiActivity context) {
        super(context);
        mcontext = context;
    }

    public XFShopCartDialog(NmXiaoFeiActivity context, int theme, List<NmXFBuyItemBean> bl) {
        super(context, theme);
        mcontext = context;
        buylist = bl;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.nm_xf_shop_cart);

        cartList = (ListView) findViewById(R.id.xf_shop_cart_list);
        xfPricetext = (TextView) findViewById(R.id.xf_cart_price);
        xfBuyNowCart = (Button) findViewById(R.id.xf_cart_buyNow) ;
        xfEmptyCart = (Button) findViewById(R.id.xf_cart_emptyCart);
        xfShopCartAdapter = new XFShopCartAdapter(mcontext,buylist);


        cartList.setItemsCanFocus(true);
        cartList.setAdapter(xfShopCartAdapter);
        cartList.post(new Runnable() {
            @Override
            public void run() {
                xfBuyNowCart.requestFocus();
                xfBuyNowCart.requestFocusFromTouch();
            }
        });

        xfEmptyCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearCart();
                xfShopCartAdapter.notifyDataSetChanged();
            }
        });

        xfBuyNowCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(buylist.size() > 0) {
                    XFBuyDialog buydialog = new XFBuyDialog(mcontext, R.style.introDialog, buylist);
                    buydialog.setCanceledOnTouchOutside(false);//设置点击Dialog外部任意区域关闭Dialog
                    buydialog.show();
                    EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_XIAOFEI + "\t" +
                            MyApplication.interfaceVersion + "\t" + "购物车下单"));
                }else{
                    ToastUtil.showShortToast(mcontext, mcontext.getResources().getString(R.string.xf_nocart_reminder), 2000);
                }
            }
        });

        setPrice();

        Button backBtn = (Button) findViewById(R.id.dialog_back);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dismiss();
            }
        });
    }

    private void setPrice(){
        String tPrice = mcontext.getCartPrice();
        xfPricetext.setText(tPrice + " " + mcontext.getResources().getString(R.string.xf_rmb));
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    private void clearCart(){
        buylist.clear();
        EventBus.getDefault().post(new XFUpdateCartEvent(false,false));
        setPrice();
    }

    //购物篮项目操作
    public void onEventMainThread(XFCartOperationCartEvent e) {
        int pos = e.getPosition();
        int count = buylist.get(pos).getNum();
        int visiblePosition = cartList.getFirstVisiblePosition();

        //得到指定位置的视图
        View view = cartList.getChildAt(pos - visiblePosition);

        TextView tx = view.findViewById(R.id.cart_list_count);

        switch (e.getFunTag()){
            case 1:
                if(count > 1){
                    buylist.get(pos).setNum(count - 1);
                    tx.setText(buylist.get(pos).getNum() + "");
                    EventBus.getDefault().post(new XFUpdateCartEvent(false,false));
                    setPrice();
                }
                break;
            case 2:
                int max = mcontext.getMaxBuyCount(buylist.get(pos).getType());
                int nextcount = mcontext.getCartCount(buylist.get(pos).getType(),1);
                if(nextcount <= max){
                    buylist.get(pos).setNum(count + 1);
                    tx.setText(buylist.get(pos).getNum() + "");
//                    xfShopCartAdapter.notifyDataSetChanged();
                    EventBus.getDefault().post(new XFUpdateCartEvent(true,false));
                    setPrice();
                }else{
                    String typeName = mcontext.getItemTypeName(buylist.get(pos).getType());
                    ToastUtil.showShortToast(mcontext, mcontext.getResources().getString(R.string.xf_nomorethan)
                            + " " + max + " " + typeName, 2000);
                }
                break;
            case 3:
                buylist.remove(pos);
                xfShopCartAdapter.notifyDataSetChanged();
                EventBus.getDefault().post(new XFUpdateCartEvent(false,false));
                setPrice();
                break;
            default:
                break;

        }
    }

    //收到下单返回,成功关闭窗口
    public void onEventMainThread(XFBuyResEvent e) {
        if (e.getTag()){
            clearCart();
            dismiss();
        }
    }
}